<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" >
            <a-form-model-item label="合同自动编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAutoNumber">
              <a-input v-model="model.contractAutoNumber" placeholder="请输入合同自动编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="主合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainContractName">
              <a-input v-model="model.mainContractName" placeholder="请输入主合同名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="主合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="mainContractNumber">
              <a-input v-model="model.mainContractNumber" placeholder="请输入主合同编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同手动编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractManualName">
              <a-input v-model="model.contractManualName" placeholder="请输入合同手动编号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractName">
              <a-input v-model="model.contractName" placeholder="请输入合同名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="委托人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="client">
              <a-input v-model="model.client" placeholder="请输入委托人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="隶属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subordinateCompany">
              <j-category-select v-model="model.subordinateCompany" pcode="B03" placeholder="请选择隶属公司"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="投资额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="investmentAmount">
              <a-input-number v-model="model.investmentAmount" placeholder="请输入投资额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="预估收入" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="estimatedIncome">
              <a-input v-model="model.estimatedIncome" placeholder="请输入预估收入" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractType">
              <j-dict-select-tag type="radio" v-model="model.contractType" dictCode="contract_type" placeholder="请选择合同类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectLeader">
              <a-input v-model="model.projectLeader" placeholder="请输入项目负责人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同履行状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractPerformanceStatus">
              <j-dict-select-tag type="radio" v-model="model.contractPerformanceStatus" dictCode="contract_performance_status" placeholder="请选择合同履行状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同签订时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractSigningTime">
              <j-date placeholder="请选择合同签订时间" v-model="model.contractSigningTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectName">
              <a-input v-model="model.projectName" placeholder="请输入项目名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detailedAddress">
              <a-input v-model="model.detailedAddress" placeholder="请输入详细地址" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目规模" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectScale">
              <a-input v-model="model.projectScale" placeholder="请输入项目规模" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目所在地" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectLocation">
              <a-input v-model="model.projectLocation" placeholder="请输入项目所在地" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectSource">
              <j-dict-select-tag type="radio" v-model="model.projectSource" dictCode="project_source" placeholder="请选择项目来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="项目参与人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectParticipants">
              <a-input v-model="model.projectParticipants" placeholder="请输入项目参与人员" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractValue">
              <a-input-number v-model="model.contractValue" placeholder="请输入合同金额" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="资金来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fundSource">
              <a-input-number v-model="model.fundSource" placeholder="请输入资金来源" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="咨询业务范围及内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="consultingBusiness">
              <a-input v-model="model.consultingBusiness" placeholder="请输入咨询业务范围及内容" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="履行期限" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="performanceTime">
              <a-input v-model="model.performanceTime" placeholder="请输入履行期限" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="履行期限说明" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="performanceTimeExplain">
              <a-textarea v-model="model.performanceTimeExplain" rows="4" placeholder="请输入履行期限说明" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="处罚条款" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="penaltyClause">
              <a-textarea v-model="model.penaltyClause" rows="4" placeholder="请输入处罚条款" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="收费标准" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="chargingStandards">
              <a-textarea v-model="model.chargingStandards" rows="4" placeholder="请输入收费标准" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="付款条件" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="paymentTerms">
              <a-textarea v-model="model.paymentTerms" rows="4" placeholder="请输入付款条件" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remarks">
              <a-textarea v-model="model.remarks" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="是否有中标通知书" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hasBidNotice">
              <j-dict-select-tag type="list" v-model="model.hasBidNotice" dictCode="has_or_not" placeholder="请选择是否有中标通知书" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="中标通知书附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bidNoticeAttachment">
              <j-upload v-model="model.bidNoticeAttachment"  ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" >
            <a-form-model-item label="合同附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contractAttachment">
              <j-upload v-model="model.contractAttachment"  ></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="合同联系人" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="contactPersonTable.loading"
          :columns="contactPersonTable.columns"
          :dataSource="contactPersonTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'ContractManagementForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        model:{
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
           contractManualName: [
              { required: true, message: '请输入合同手动编号!'},
           ],
           contractName: [
              { required: true, message: '请输入合同名称!'},
           ],
           client: [
              { required: true, message: '请输入委托人!'},
           ],
           subordinateCompany: [
              { required: true, message: '请输入隶属公司!'},
           ],
           contractType: [
              { required: true, message: '请输入合同类型!'},
           ],
           contractPerformanceStatus: [
              { required: true, message: '请输入合同履行状态!'},
           ],
           contractSigningTime: [
              { required: true, message: '请输入合同签订时间!'},
           ],
           contractAttachment: [
              { required: true, message: '请输入合同附件!'},
           ],
        },
        refKeys: ['contactPerson', ],
        tableKeys:['contactPerson', ],
        activeKey: 'contactPerson',
        // 合同联系人
        contactPersonTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '姓名',
              key: 'name',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '职位',
              key: 'position',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '手机',
              key: 'mobile',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '电话',
              key: 'phone',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: 'QQ',
              key: 'qq',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        url: {
          add: "/bid/contractManagement/add",
          edit: "/bid/contractManagement/edit",
          queryById: "/bid/contractManagement/queryById",
          contactPerson: {
            list: '/bid/contractManagement/queryContactPersonByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
      addBefore(){
        this.contactPersonTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.contactPerson.list, params, this.contactPersonTable)
        }
      },
      //校验所有一对一子表表单
      validateSubForm(allValues){
          return new Promise((resolve,reject)=>{
            Promise.all([
            ]).then(() => {
              resolve(allValues)
            }).catch(e => {
              if (e.error === VALIDATE_NO_PASSED) {
                // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
              } else {
                console.error(e)
              }
            })
          })
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          contactPersonList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     handleCategoryChange(value,backObj){
      this.model = Object.assign(this.model, backObj);
      }

    }
  }
</script>

<style scoped>
</style>